/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elaborate.h2x.transform.position.schema.state;

import java.util.Enumeration;
import java.util.HashSet;
import juixb.schema.ChoiceOperator;
import juixb.schema.SchemaNode;
import juixb.schema.SchemaTreeNode;

/**
 *
 * @author lendle
 */
public class ChoiceStateParser implements StateParser {

    public boolean isApplicable(SchemaNode schemaNode) {
        return schemaNode instanceof ChoiceOperator;
    }

    public State parseState(StateParsers parsers, SchemaNode schemaNode) {
        DefaultPseudoStateImpl state=new DefaultPseudoStateImpl();
        HashSet<State> firstSet=new HashSet<State>();
        HashSet<State> lastSet=new HashSet<State>();
        Enumeration<SchemaTreeNode> en=schemaNode.children();
        while(en.hasMoreElements()){
            SchemaNode child=(SchemaNode) en.nextElement();
            State childState=parsers.parseState(child);
            state.addChildState(childState);
            firstSet.addAll(childState.getFirstSet());
            lastSet.addAll(childState.getLastSet());
        }
        
        state.setFirstSet(firstSet);
        state.setLastSet(lastSet);
        return state;
    }
    
}
